/**
 * 通用js方法封装处理
 * Copyright (c) 2019 ruoyi
 */

const baseURL = process.env.VUE_APP_BASE_API

// 日期格式化
export function parseTime(time, pattern) {
	if (arguments.length === 0 || !time) {
		return null
	}
	const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
			time = parseInt(time)
		}
		if ((typeof time === 'number') && (time.toString().length === 10)) {
			time = time * 1000
		}
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
  return format.replace(/{([ymdhisa])+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
}

// 表单重置
export function resetForm(refName) {
	if (this.$refs[refName]) {
		this.$refs[refName].resetFields();
	}
}

// 新增日期范围
export function addDateRange(params, dateRange) {
	var search = params;
	search.beginTime = "";
	search.endTime = "";
	if (null != dateRange && '' !== dateRange) {
		search.beginTime = this.dateRange[0];
		search.endTime = this.dateRange[1];
	}
	return search;
}

// 回显数据字典
export function selectDictLabel(datas, value) {
  if(!datas){
    return ''
  }
	var actions = [];
	Object.keys(datas).map((key) => {
		if (('' + datas[key].dictValue) === ('' + value)) {
			actions.push(datas[key].dictLabel);
			return false;
		}
	})
	return actions.join('');
}

// 回显数据字典数组
export function selectDictLabelMultiArray(datas, value) {
  var actions = [];
  var arr = value.split(",")
  arr.forEach(item => {
    Object.keys(datas).map((key) => {
      if (datas[key].dictValue === ('' + item)) {
        actions.push(datas[key].dictLabel);
        return false;
      }
    })
  })

  return actions.join('，');
}

// 回显部门名称
export function selectDeptName(datas, value, fullShow = true) {
  let actions = []
  if (value && value.length > 0 && datas && datas.length > 0) {
    let tempData = datas
    for (let i = 0; i < value.length; i++) {
      for (let j = 0; j < tempData.length; j++) {
        if (tempData[j].deptId === ('' + value[i])) {
          actions.push(tempData[j].deptName)
          tempData = tempData[j].children
          break
        }
      }
    }
  }
  if (fullShow) {
    return actions.join('-')
  } else {
    return actions[actions.length - 1] + ''
  }
}

// 回显部门名称
export function selectDeptNameByTree(datas, value, fullShow = true) {
  let actions = []
  if (value && value.length > 0 && datas && datas.length > 0) {
    let tempData = datas
    for (let i = 0; i < value.length; i++) {
      for (let j = 0; j < tempData.length; j++) {
        if (tempData[j].id === ('' + value[i])) {
          actions.push(tempData[j].label)
          tempData = tempData[j].children
          break
        }
      }
    }
  }
  if (fullShow) {
    return actions.join('--')
  } else {
    return actions[actions.length - 1] + ''
  }
}

// 处理图片路径
export function handlePicturePath(path) {
  let array = [];
  if(path){
    array = [];
    array = path.toString().split(",");
    for(let i = 0; i < array.length; i++){
      array[i] = array[i];
    }
  }
  return array;
}

// 处理照片墙的回显图片路径
export function handleFileList(pathArray) {
  let array = [];
  if(pathArray && pathArray.length > 0){
    for(let i = 0; i < pathArray.length; i++){
      let file = {};
      file['uid'] = guid();
      file['url'] = pathArray[i];
      array.push(file);
    }
  }
  return array;
}

export function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
  });
}

// 处理单图片路径
export function handlePicturePathSingle(path) {
  let res = '';
  if(path){
      res = process.env.VUE_APP_PICTURE_PATH + path;
  }
  return res;
}

// 通用下载方法
export function download(fileName) {
	window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + false;
}

// 字符串格式化(%s )
export function sprintf(str) {
	var args = arguments, flag = true, i = 1;
	str = str.replace(/%s/g, function () {
		var arg = args[i++];
		if (typeof arg === 'undefined') {
			flag = false;
			return '';
		}
		return arg;
	});
	return flag ? str : '';
}

// 转换字符串，undefined,null等转化为""
export function praseStrEmpty(str) {
    if (!str || str === "undefined" || str === "null") {
        return "";
    }
    return str;
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data, id, parentId, children) {
  let config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children'
  };

  var childrenListMap = {};
  var nodeIds = {};
  var tree = [];

  for (let d of data) {
    let parentId = d[config.parentId];
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = [];
    }
    nodeIds[d[config.id]] = d;
    childrenListMap[parentId].push(d);
  }

  for (let d of data) {
    let parentId = d[config.parentId];
    if (nodeIds[parentId] == null) {
      tree.push(d);
    }
  }

  for (let t of tree) {
    adaptToChildrenList(t);
  }

  function adaptToChildrenList(o) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]];
    }
    if (o[config.childrenList]) {
      for (let c of o[config.childrenList]) {
        adaptToChildrenList(c);
      }
    }
  }
  return tree;
}

export function formatterType (row, column, cellvalus, index) {
  // console.log(row)
  // console.log(column)
  // console.log(cellvalus)
  // console.log(index)
  if (cellvalus && cellvalus !== '') {
    return cellvalus
  }
  else return '---';
}


//身份证隐藏中间数
export function hideIdCardMiddle (val) {
  window.console.log(val)
  if (!val) {
    return '';
  }
  if (val.length === 0) {
    return '';
  }
  else if (val.length <= 2) {
    return val + '';
  }
  else if (val.length <= 6) {
    return val.substring(0, 1) + '********' + val.substring(val.length - 1)
  }
  else {
    return val.substring(0, 6) + '********' + val.substring(val.length - 4)
  }

}


/**
 *
 * @returns {number}
 */
export function getRatio() {
  /*var ratio=0;
  var screen=window.screen;
  var ua=navigator.userAgent.toLowerCase();

  if(window.devicePixelRatio !== undefined)
  {
    ratio=window.devicePixelRatio;
  }
  else if(~ua.indexOf('msie'))
  {
    if(screen.deviceXDPI && screen.logicalXDPI)
    {
      ratio=screen.deviceXDPI/screen.logicalXDPI;
    }

  }
  else if(window.outerWidth !== undefined && window.innerWidth !== undefined)
  {
    ratio=window.outerWidth/window.innerWidth;
  }

  if(ratio)
  {
    ratio=Math.round(ratio*100);
  }
  return ratio/100;*/
  return 1;
}


// 数据合并
export function mergeRecursive(source, target) {
  for (var p in target) {
    try {
      if (target[p].constructor === Object) {
        source[p] = mergeRecursive(source[p], target[p]);
      } else {
        source[p] = target[p];
      }
    } catch (e) {
      source[p] = target[p];
    }
  }
  return source;
}
